package com.po;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * BaseMetadata entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.po.BaseMetadata
 * @author MyEclipse Persistence Tools
 */

public class BaseMetadataDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(BaseMetadataDAO.class);
	// property constants
	public static final String PARENT_META_ID = "parentMetaId";
	public static final String CHINESE_NAME = "chineseName";
	public static final String ENGLISH_NAME = "englishName";
	public static final String SHORT_NAME = "shortName";
	public static final String DEFINES = "defines";
	public static final String CONDITIONS = "conditions";
	public static final String MAX_FREQUENCY = "maxFrequency";
	public static final String DATA_TYPE = "dataType";
	public static final String AREA = "area";
	public static final String REMARK = "remark";
	public static final String LEVELS = "levels";
	public static final String CLASSIFY = "classify";
	public static final String LINK_KEY = "linkKey";
	public static final String REQUIRING = "requiring";

	public void save(BaseMetadata transientInstance) {
		log.debug("saving BaseMetadata instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(BaseMetadata persistentInstance) {
		log.debug("deleting BaseMetadata instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public BaseMetadata findById(java.lang.String id) {
		log.debug("getting BaseMetadata instance with id: " + id);
		try {
			BaseMetadata instance = (BaseMetadata) getSession().get(
					"com.po.BaseMetadata", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(BaseMetadata instance) {
		log.debug("finding BaseMetadata instance by example");
		try {
			List results = getSession().createCriteria("com.po.BaseMetadata")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BaseMetadata instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from BaseMetadata as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByParentMetaId(Object parentMetaId) {
		return findByProperty(PARENT_META_ID, parentMetaId);
	}

	public List findByChineseName(Object chineseName) {
		return findByProperty(CHINESE_NAME, chineseName);
	}

	public List findByEnglishName(Object englishName) {
		return findByProperty(ENGLISH_NAME, englishName);
	}

	public List findByShortName(Object shortName) {
		return findByProperty(SHORT_NAME, shortName);
	}

	public List findByDefines(Object defines) {
		return findByProperty(DEFINES, defines);
	}

	public List findByConditions(Object conditions) {
		return findByProperty(CONDITIONS, conditions);
	}

	public List findByMaxFrequency(Object maxFrequency) {
		return findByProperty(MAX_FREQUENCY, maxFrequency);
	}

	public List findByDataType(Object dataType) {
		return findByProperty(DATA_TYPE, dataType);
	}

	public List findByArea(Object area) {
		return findByProperty(AREA, area);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByLevels(Object levels) {
		return findByProperty(LEVELS, levels);
	}

	public List findByClassify(Object classify) {
		return findByProperty(CLASSIFY, classify);
	}

	public List findByLinkKey(Object linkKey) {
		return findByProperty(LINK_KEY, linkKey);
	}

	public List findByRequiring(Object requiring) {
		return findByProperty(REQUIRING, requiring);
	}

	public List findAll() {
		log.debug("finding all BaseMetadata instances");
		try {
			String queryString = "from BaseMetadata";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public BaseMetadata merge(BaseMetadata detachedInstance) {
		log.debug("merging BaseMetadata instance");
		try {
			BaseMetadata result = (BaseMetadata) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(BaseMetadata instance) {
		log.debug("attaching dirty BaseMetadata instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(BaseMetadata instance) {
		log.debug("attaching clean BaseMetadata instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}